<template>
  <div class="my-container">
    <!-- 登录后 -->
    <div v-if="user" class="header user-info">
      <div class="base-info">
        <div class="left">
          <van-image class="avatar" :src="userInfo.photo" round fit="cover" />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="fight">
          <van-button size="mini" round>编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{ userInfo.art_count }}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.follow_count }}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.fans_count }}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.like_count }}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!-- /登录后 -->
    <!-- 登录前 -->
    <div v-else class="header not-login">
      <div class="login-but" @click="$router.push('/login')">
        <img src="~@/assets/mobile.png" alt="" class="mobile-img" />
        <span class="text"> 登录 / 注册 </span>
      </div>
    </div>
    <!-- /登录前 -->
    <!-- Grid 宫格 收藏 历史-->
    <van-grid :column-num="2" class="gridNav" clickable>
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 、Grid 宫格 收藏 历史-->
    <!-- 登录导航后 -->
   <div>
      <div class="grid-nav" v-if="user">
      <van-cell title="消息通知" is-link />
      <van-cell title="实名认证" is-link />
      <van-cell title="用户反馈" is-link />
      <van-cell title="小智同学" is-link />
      <van-cell title="系统设置" is-link />
    </div>
    <!-- /登录导航后 -->
      <!-- 登录导航前 -->
    <div v-else class="grid-nav">
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
    </div>
    <!-- /登录导航前 -->
    <van-cell
      v-if="user"
      class="logout-cell"
      title="退出登录"
      center
      clickable
      @click="noLogin"
    />
   </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'
export default {
  name: 'MyIndex',
  data() {
    return {
      userInfo: {} // 用户信息
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
    if (this.user) {
      this.loadUserInfo()
    }
  },
  methods: {
    noLogin() {
      this.$dialog
        .confirm({
          title: '确认退出吗 ？'
        })
        .then(() => {
          // on confirm
          // console.log('确认退出')
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
          // console.log('取消退出')
        })
    },
    async loadUserInfo() {
      try {
        const { data } = await getUserInfo()
        this.userInfo = data.data
      } catch (err) {
        this.$toast('获取数据失败，请稍后重试')
      }
    }
  }
}
</script>
<style lang="less" scoped>
.my-container {
  .header {
    height: 200px;
    background: url(~@/assets/banner.png);
    background-size: cover;
    .mobile-img {
      width: 66px;
      height: 66px;
    }
    .text {
      margin-top: 8px;
      font-size: 14px;
      color: #fff;
    }
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-but {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
  }
  .user-info {
    .base-info {
      height: 147px;
      padding: 50px 16px 16px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 66px;
          height: 66px;
          margin-right: 12px;
        }
        .name {
          font-size: 15px;
          color: #fff;
        }
      }
      .fight {
        display: flex;
        align-items: center;
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        height: 50px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          font-size: 20px;
        }
        .text {
          font-size: 15px;
        }
      }
    }
  }
  .gridNav {
    margin-bottom: 10px;
    .toutiao {
      font-size: 25px;
      margin-bottom: 15px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    span {
      font-size: 14px;
    }
  }
  .logout-cell {
    height: 50px;
    text-align: center;
    color: #eb5253;
    margin: 10px 0;
    font-size: 15px;
  }
}
</style>
